//
//  YLWebViewTableViewCell.m
//  YLFinanceService
//
//  Created by she1323 on 2017/9/6.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLWebViewTableViewCell.h"

@implementation YLWebViewTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

-(id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        _loadSubject = [RACSubject subject];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        //NSLog(@"%@",NSStringFromCGRect(self.contentView.bounds));
        
        // 高度必须提前赋一个值 >0
        self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, 1)];
        self.webView.backgroundColor = [UIColor clearColor];
        self.webView.opaque = NO;
        self.webView.userInteractionEnabled = NO;
        self.webView.scrollView.bounces = NO;
        self.webView.delegate = self;
        self.webView.paginationBreakingMode = UIWebPaginationBreakingModePage;
        self.webView.scalesPageToFit = YES;
        [self.contentView addSubview:self.webView];
    }
    return self;
}

#pragma mark - UIWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView

{
    
//    if (webView.isLoading) {
//        return;
//    }
    // 如果要获取web高度必须在网页加载完成之后获取
    
    // 方法一
    float height = [[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollHeight"]floatValue];
    // 用通知发送加载完成后的高度
    webView.frame = CGRectMake(0, 0, YLScreenW, height);
    webView.backgroundColor = [UIColor redColor];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"WEBVIEW_HEIGHT" object:self userInfo:@{@"height" : @(height)}];
    
}

// contentStr 用于更新值
-(void)setContentStr:(NSString *)contentStr
{
    if (_contentStr == contentStr) {
        return;
    }
    
    _contentStr = contentStr;
    
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:contentStr]]];
}



@end
